var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    positionInfo:{},
    position:"",
    age:"",
    company:"",
    phone:"",
    name:"",
    comment:"",
    searchValue:{
      id:'',
      name:''
    },
    searchPosiValue:{
      id:'',
      name:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      positionInfo: JSON.parse(options.position)
    });
  },

  //关闭成功提示
  closeTap: function () {
    this.setData({
      mode: true,
      height: "100%"
    })
  },

  ageInput: function (e) {
    this.setData({
      age: e.detail.value
    });
  },
  positionInput: function (e) {
    this.setData({
      position: e.detail.value
    });
  },
  companyInput: function (e) {
    this.setData({
      company: e.detail.value
    });
  },
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    });
  },
  commentInput:function(e){
    this.setData({
      comment:e.detail.value
    });
  },
  isSendTap: function (e) {
    var formId = e.detail.formId;
    if (this.data.name == '' || this.data.name == undefined) {
      app.alert('候选人姓名不能为空！')
      return false;
    }
    if (this.data.phone == '' || this.data.phone == undefined) {
      app.alert('候选人手机号码不能为空！')
      return false;
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      app.alert('手机号码格式不对！')
      return false;
    } 
    if (this.data.searchValue.name == '' || this.data.searchValue.name == undefined) {
      app.alert('候选人所在公司不能为空！')
      return false;
    }
    if (this.data.searchPosiValue.name == '' || this.data.searchPosiValue.name == undefined) {
      app.alert('候选人所在职位不能为空！')
      return false;
    }
    if (this.data.age == '' || this.data.age == undefined) {
      app.alert('候选人年龄不能为空！')
      return false;
    } else if (!(/^[0-9]*$/.test(this.data.age))) {
      app.alert('候选人年龄格式不对！')
      return false;
    }
    // console.log(this.data.name);
    // console.log(this.data.age);
    // console.log(this.data.searchValue.name);
    // console.log(this.data.searchPosiValue.name);
    // console.log(this.data.phone);
    // console.log(this.data.positionInfo.id);
    // console.log(this.data.comment);
    // console.log(app.globalData.openid);
    if (this.data.name || this.data.age || this.data.searchValue.name || this.data.searchPosiValue.name || this.data.phone) {
      util.post(app.api.addRecommend, {
        name: this.data.name,
        age: this.data.age,
        companyname: this.data.searchValue.name,
        position: this.data.searchPosiValue.name,
        phone: this.data.phone,
        comment: this.data.comment,
        unionid: app.globalData.unionid,
        openid: app.globalData.openid,
        jobid: this.data.positionInfo.id,
        roleid: this.data.positionInfo.roleid,
        formid:formId
      }, function (res) {
        console.log(res.data);
        if (res.data) {
          // console.log(res.data);
          // console.log(res.data.code);
          // console.log(res.data.data);
          // console.log(res.code);
          // console.log(res.data);
          if (res.code==200){
            // console.log(res.data.msg);
            // console.log(res.msg);
            app.alert(res.msg)
            return false;
          } else if(res.code==200&&res.data>1){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateTo({
                  url: '../my-position/my-position',
                });
              }, 500);
            }
          })
          }
        } else {
          app.alert("出错error");
        }
      })
    } else {
      app.alert("请填写完整意见信息");
    }
  },

  //公司详情
  companyDetailTap: function (event) {
    var id = event.currentTarget.dataset.pid; // 当前id
    wx.navigateTo({
      url: '../company-detail/company-detail?id=' + id
    });
  },


  //1标识为公司
  goCompanySearchTap:function(e){
    wx.navigateTo({
      url: '../search/search?cid='+1,
    })
  },

  //2标识为职位
  goPosiSearchTap:function(e){
    wx.navigateTo({
      url: '../search/search?pid='+2,
    })
  },

  //分享
  onShareAppMessage: function (res) {
    var _this = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    var title = this.data.positionInfo.enterprise_name + ' 招聘 ' + this.data.positionInfo.name + '【推荐奖金' + this.data.positionInfo.reward + '】';
    return {
      title: title,
      path: 'pages/position-detail/position-detail',
      data: _this.data.positionInfo,
      success: function (res) {
        // 转发成功

      },
      fail: function (res) {
    
        // 转发失败
      }
    }
  }
})